<?php

namespace Apps\AppApi\Method\User;


use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Libs\MobileSMS;
use Apps\Common\Models\UserBase;

class UpdatePassword extends BaseMethod
{
    public function run()
    {
        $mobile = $this->getDataItem('mobile');
        $password = $this->getDataItem('password');
        $smsCode = $this->getDataItem('sms_code');

        if (empty($mobile) || empty($password) || empty($smsCode)) {
            $this->setResult([
                'success' => 0,
                'message' => '缺少必传参数.'
            ]);
            return false;
        }

        if(!$this->userInfoByAccount($mobile)){
            $this->setResult([
                'success' => 0,
                'message' => '手机号码不存在.'
            ]);
            return false;
        }
        if(!MobileSMS::Check('update_password', $smsCode, $mobile, $this->apiAccessToken->id)) {
            $this->setResult([
                'success' => 0,
                'message' => '短信验证码错误.'
            ]);
            return false;
        }
        $newPassword = $this->hashPassword($password);
        if(UserBase::SetPassword($mobile, $newPassword)) {
            $this->setResult([
                'success' => 1,
                'message' => '修改密码成功.'
            ]);
        } else {
            $this->setResult([
                'success' => 0,
                'message' => '保存数据异常.'
            ]);
        }
    }
}